package com.quxiang.pay.service;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.quxiang.pay.dao.CymjDao;
import com.quxiang.pay.dao.MaqueDao;
import com.quxiang.pay.dao.XunLeDao;

@Service
public class TaskServiceImpl implements TaskService {

	private Logger logger = LoggerFactory.getLogger(TaskServiceImpl.class);
	@Autowired
	private CymjDao cymjDao;

	@Autowired
	private XunLeDao xunleDao;
	@Autowired
	private MaqueDao maqueDao;

	@Transactional(rollbackFor = Exception.class, value = "cymj_transactionManager")
	public void computeCymj() throws Exception {

		logger.info("cymj 代理商返点任务开始..");
		Map<String, String> task = cymjDao.selectOne("task.getTask");
		if (task == null || task.isEmpty()) {
			logger.info("cymj 代理商返点任务 不需要执行..");
			return;
		}
		List<Map<String, Object>> orderList = cymjDao.selectList("task.getAllOrder");
		if (orderList != null) {
			logger.info("cymj 代理商返点任务 taskCount = {}", orderList.size());
			for (Map<String, Object> order : orderList) {

				int userId = Integer.parseInt(String.valueOf(order.get("userid"))); // 充值用户
				long money = Long.parseLong(String.valueOf(order.get("order_fee"))); // 充值金额
				long orderId = Long.parseLong(String.valueOf(order.get("order_id")));// 充值订单号
				logger.info("cymj 代理商返点任务 userid = {} money = {},orderId = {} ", userId, money, orderId);
				// 目前按照固定的三级代理进行 默认 直接40% 5% 3 %
				int level = 1;
				Map<String, Object> parent = null;
				// 1.获取第一级
				while (true) {
					parent = cymjDao.selectOne("task.getParent", userId);

					if (parent == null || parent.size() == 0) {

						break;
					}
					long agentId = Integer.parseInt(String.valueOf(parent.get("agentId")));
					int rate = parent.get("p_rate") == null ? 0 : (int) parent.get("p_rate");
					if (agentId == -99) {
						logger.debug("已经到了最上级");
						break;
					}
					if (rate == 0) {
						if (level == 1) {
							rate = 40;
						} else if (level == 2) {
							rate = 5;
						} else if (level == 3) {
							rate = 3;
						}
					}

					long add = money * rate / 100;
					if(add != 0){
						parent.put("add", add);
						// 更新代理金额
						cymjDao.update("task.updateAgentMoney", parent);
						parent.putAll(order);
						parent.put("p_rate", rate);
						parent.put("resource_user_id", userId);
						cymjDao.insert("task.insertAgentMoney", parent);
					}
					
					parent = null;
					level++;
					userId = cymjDao.selectOne("task.getUserId", agentId);
					// 写入日志数据

				}

				cymjDao.update("task.updateOrder", orderId);

			}
		} else {
			logger.info("cymj 代理商返点任务 orderList is null..");
		}

	}

	@Override
	@Transactional(rollbackFor = Exception.class, value = "cymj_transactionManager")
	public void computeXunle() throws Exception {

		logger.info("xunle 代理商返点任务开始..");
		Map<String, String> task = xunleDao.selectOne("task.getTask");
		if (task == null || task.isEmpty()) {
			logger.info("xunle 代理商返点任务 不需要执行..");
			return;
		}
		List<Map<String, Object>> orderList = xunleDao.selectList("task.getAllOrder");
		if (orderList != null) {
			logger.info("xunle 代理商返点任务 taskCount = {}", orderList.size());
			for (Map<String, Object> order : orderList) {

				int userId = Integer.parseInt(String.valueOf(order.get("userid")));
				long money = Long.parseLong(String.valueOf(order.get("order_fee")));
				long orderId = Long.parseLong(String.valueOf(order.get("order_id")));
				logger.info("xunle 代理商返点任务 userid = {} money = {},orderId = {} ", userId, money, orderId);
				// 目前按照固定的三级代理进行 默认 直接40% 5% 3 %
				int level = 1;
				Map<String, Object> parent = null;
				// 1.获取第一级
				while (true) {
					parent = xunleDao.selectOne("task.getParent", userId);

					if (parent == null || parent.size() == 0) {

						break;
					}
					long agentId = Integer.parseInt(String.valueOf(parent.get("agentId")));
					int rate = parent.get("p_rate") == null ? 0 : (int) parent.get("p_rate");
					if (agentId == -99) {
						logger.debug("已经到了最上级");
						break;
					}
					if (rate == 0) {
						if (level == 1) {
							rate = 40;
						} else if (level == 2) {
							rate = 5;
						} else if (level == 3) {
							rate = 3;
						}
					}

					long add = money * rate / 100;
					if(add != 0 ){
						parent.put("add", add);
						// 更新代理金额
						xunleDao.update("task.updateAgentMoney", parent);
						parent.putAll(order);
						parent.put("p_rate", rate);
						parent.put("resource_user_id", userId);
						xunleDao.insert("task.insertAgentMoney", parent);
					}
					
					parent = null;
					level++;
					userId = xunleDao.selectOne("task.getUserId", agentId);
					// 写入日志数据

				}

				xunleDao.update("task.updateOrder", orderId);

			}
		} else {
			logger.info("xunle 代理商返点任务 orderList is null..");
		}

	}

	@Transactional(rollbackFor = Exception.class, value = "maque_transactionManager")
	public void computeMaque() throws Exception {

		logger.info("maque 代理商返点任务开始..");
		Map<String, String> task = maqueDao.selectOne("task.getTask");
		if (task == null || task.isEmpty()) {
			logger.info("maque 代理商返点任务 不需要执行..");
			return;
		}
		List<Map<String, Object>> orderList = maqueDao.selectList("task.getAllOrder");
		if (orderList != null) {
			logger.info("maque 代理商返点任务 taskCount = {}", orderList.size());
			for (Map<String, Object> order : orderList) {

				int userId = Integer.parseInt(String.valueOf(order.get("userid"))); // 充值用户
				long money = Long.parseLong(String.valueOf(order.get("order_fee"))); // 充值金额
				long orderId = Long.parseLong(String.valueOf(order.get("order_id")));// 充值订单号
				logger.info("maque 代理商返点任务 userid = {} money = {},orderId = {} ", userId, money, orderId);
				// 目前按照固定的三级代理进行直属40%，二级7%，三级3%
				int level = 1;
				Map<String, Object> parent = null;
				// 1.获取第一级
				while (true) {
					parent = maqueDao.selectOne("task.getParent", userId);

					if (parent == null || parent.size() == 0) {

						break;
					}
					long agentId = Integer.parseInt(String.valueOf(parent.get("agentId")));
					int rate = parent.get("p_rate") == null ? 0 : (int) parent.get("p_rate");
					if (agentId == -99) {
						logger.debug("已经到了最上级");
						break;
					}
					if (rate == 0) {
						if (level == 1) {
							rate = 40;
						} else if (level == 2) {
							rate = 7;
						} else if (level == 3) {
							rate = 3;
						}
					}

					long add = money * rate / 100;
					if (add != 0) {

						parent.put("add", add);
						// 更新代理金额
						maqueDao.update("task.updateAgentMoney", parent);
						parent.putAll(order);
						parent.put("p_rate", rate);
						parent.put("resource_user_id", userId);
						maqueDao.insert("task.insertAgentMoney", parent);
					}

					parent = null;
					level++;
					userId = maqueDao.selectOne("task.getUserId", agentId);
					// 写入日志数据

				}

				maqueDao.update("task.updateOrder", orderId);

			}
		} else {
			logger.info("maque 代理商返点任务 orderList is null..");
		}

	}

}
